package xyz.fping.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import xyz.fping.service.PaymentHystrixService;

import javax.annotation.Resource;

/**
 * OrderHystrixController
 *
 * @author fping
 * @date 2020/11/8 11:29
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globalFallbackMethod")
public class OrderHystrixController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfoOK(id);
        log.info("result: " + result);
        return result;
    }

    @GetMapping("/consumer/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })*/
    @HystrixCommand
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfoTimeout(id);
        log.info("result: " + result);
        return result;
    }

    public String paymentInfoTimeoutHandler(Integer id) {
        return "我是消费者80，对方支付系统繁忙，请稍后再试，/(ㄒoㄒ)/~~";
    }

    /**
     * 自定义全局fallback方法
     *
     * @return
     */
    public String globalFallbackMethod() {
        return "Global异常信息：请稍后再试 /(ㄒoㄒ)/~~";
    }
}
